package malus.shaofeng.com.beiwomusic.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.List;

import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.activity.BBSTopicActivity;
import malus.shaofeng.com.beiwomusic.model.HotPost;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.Constants;

/**
 * Created by Administrator on 2015/3/10.
 */
public class HotPostAdapter extends BaseAdapter {
    private Context context;
    private List<HotPost> data;
    public HotPostAdapter(Context context,List<HotPost> data){
        if(context!=null){
            this.context=context;
            this.data=data;
        }else{
            throw new IllegalArgumentException("context must not null");
        }
    }
    @Override
    public int getCount() {
        if(data!=null){
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(data!=null){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if(convertView!=null){
            view=convertView;
        }else{
            view= LayoutInflater.from(context).inflate(R.layout.item_bbs_top_30_list,parent,false);
        }
        ViewHolder holder=(ViewHolder)view.getTag();
        if(holder==null){
            holder=new ViewHolder();
            holder.title=(TextView)view.findViewById(R.id.bbs_hot_list_title);
            holder.nickName=(TextView)view.findViewById(R.id.bbs_hot_list_nickname);
            holder.replise=(TextView)view.findViewById(R.id.bbs_hot_list_replies);
            holder.icon=(ImageView)view.findViewById(R.id.bbs_hot_list_icon);
        }
        HotPost hotPost = data.get(position);
        holder.title.setText(hotPost.getTitle()+"");
        holder.nickName.setText(hotPost.getUser().getNickname()+"");
        holder.replise.setText(hotPost.getReplies()+"");
        String uri = hotPost.getImage_uri();
        if (uri != null) {
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            bitmapUtils.display(holder.icon, hotPost.getImage_uri(), new BitmapLoadCallBack<ImageView>() {
                @Override
                public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                    imageView.setImageBitmap(bitmap);
                }

                @Override
                public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                }
            });
        }
        final int pos=position;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HotPost topic = data.get(pos);
                Intent intent = new Intent(context, BBSTopicActivity.class);
                intent.putExtra(Constants.BundleKey.KEY_BBS_TOPIC,topic);
                context.startActivity(intent);
                ((Activity)context).overridePendingTransition(R.anim.alpha_in,0);
            }
        });
        return view;
    }
    private class ViewHolder{
        TextView title;
        TextView nickName;
        TextView replise;
        ImageView icon;
    }
}
